//
//  WDInAppPurchaseHandleManager.m
//  WDPolymerizeSDK
//
//  Created by little People on 2023/6/16.
//

#import "WDInAppPurchaseHandleManager.h"
#import "WDAdStatusManager.h"
#import "WDJsonManager.h"
@implementation WDInAppPurchaseHandleManager
static WDInAppPurchaseHandleManager *_InAppPurchaseHandleManager = nil;
+ (instancetype)shareInAppPurchaseHandleManager
{
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _InAppPurchaseHandleManager = [[self alloc] init];
    });
    return _InAppPurchaseHandleManager;
}

- (void)billing_purchase_request:(NSString *)productId
{
    [[WDAdStatusManager shareStatusManager] inAppPurchaseRequestWithEvent:BILLING_EVENT_NAME productId:productId];
}

- (void)billing_purchase_success:(NSString *)productId
{
    [[WDAdStatusManager shareStatusManager] inAppPurchaseSuccessWithEvent:BILLING_EVENT_NAME productId:productId];
}

- (void)billing_purchase_failed:(SKPaymentTransaction *)tran errorMsg:(NSString *)msg
{
    [[WDAdStatusManager shareStatusManager] inAppPurchaseFailedWithEvent:BILLING_EVENT_NAME productId:tran.payment.productIdentifier errorType:[self productType:tran.payment.productIdentifier] errorMsg:msg];
}

- (void)billing_Revenue:(NSString  *)productIdentifier adnTransaction_id:(NSString *)transaction_id{


    
    NSNumber *num_revenue = [[NSNumber alloc] initWithFloat:[[WDJsonManager renvenueWithProductId:productIdentifier] floatValue]*1000];
    
    [[WDAdStatusManager shareStatusManager] inAppPurchaseRevenueWithEvent:BILLING_EVENT_NAME type:[self productType:productIdentifier] source:productIdentifier d_revenue:num_revenue currency:@"USD"];
    
}



- (NSString *)productType:(NSString *)productId
{
    NSString *type = @"0";
    if ([[WDJsonManager consumableProductIdForSDKConfigFile] containsObject:productId])
    {
        type = @"1";
    }
    if ([[WDJsonManager nonConsumableProductIdForSDKConfigFile] containsObject:productId] || [[WDJsonManager subscribeProductIdForSDKConfigFile] containsObject:productId])
    {
        type = @"2";
    }
    return type;
}

- (NSDictionary *)skproductInfo:(NSString *)productId
{
    for (SKProduct *pro in self.productIds) {
        NSString *pid = pro.productIdentifier;
        if ([pid isEqualToString:productId])
        {
            NSString *currency = pro.priceLocale.currencyCode;
            NSString *price = [NSString stringWithFormat:@"%@",pro.price];
            return @{@"currency":currency,@"price":price,@"title":pro.localizedTitle};
        }
        
    }
    return @{@"currency":@"",@"price":@"",@"title":@""};
}

@end
